package cn.com.zero.manager.auth.controller;

import cn.com.zero.api.log.annotation.Log;
import cn.com.zero.base.vo.ResponseVO;
import cn.com.zero.manager.auth.constant.LogType;
import cn.com.zero.manager.auth.constant.ValidationRule;
import cn.com.zero.manager.auth.po.MenuPO;
import cn.com.zero.manager.auth.po.RolePO;
import cn.com.zero.manager.auth.service.IRoleService;
import cn.com.zero.web.controller.BaseController;
import cn.com.zero.web.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.util.List;

/**
 * @author guoxin
 * @version 1.0.0
 * @Description
 * @date 2023/4/10 10:36
 */
@Api(tags = "角色数据接口")
@Validated
@RestController
@RequestMapping("/manager/role")
public class RoleController extends BaseController {

    /**
     * 角色管理service
     */
    private IRoleService roleService;

    @ApiOperation(value = "查询角色列表", notes = "查询所有角色信息集合")
    @GetMapping("/list")
    public List<RolePO> queryRoleList() {
        return roleService.queryRoleList();
    }

    @ApiOperation(value = "验证角色名称是否存在,返回对象中key对应值为true,false")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "roleName", required = true, value = "角色名称，true 角色名称已存在、false 角色名称不存在")
    })
    @GetMapping("/valid/isExistName/{roleName}")
    public ResponseVO isExistName(@PathVariable("roleName") String roleName) {
        ResponseVO responseVO = new ResponseVO();
        responseVO.setData(roleService.isExistName(roleName));
        return responseVO;
    }

    @ApiOperation(value = "验证角色编号是否存在,返回对象中key对应值为true,false")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "roleName", required = true, value = "角色名称，true 角色名称已存在、false 角色名称不存在")
    })
    @GetMapping("/valid/isExistRoleCode/{roleCode}")
    public ResponseVO isExistRoleCode(@PathVariable("roleCode") String roleCode) {
        ResponseVO responseVO = new ResponseVO();
        responseVO.setData(roleService.isExistRoleCode(roleCode));
        return responseVO;
    }

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/menuList")
    public List<MenuPO> queryMenuList() {
        return roleService.queryMenuList();
    }

    @ApiOperation(value = "查询属于某个系统的所有菜单")
    @ApiImplicitParam(paramType = "path", name = "systemId", required = true, value = "系统编号，用户查询指定系统的菜单编号集合")
    @GetMapping("/menuList/{systemId}")
    public List<MenuPO> querySystemMenuList(@PathVariable("systemId") String systemId) {
        return roleService.querySystemMenuList(systemId);
    }

    @Log(content = "添加角色", logType = LogType.LOG_MANAGE)
    @ApiOperation(value = "添加角色")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roleCode", required = true, value = "角色编号"),
            @ApiImplicitParam(paramType = "query", name = "roleName", required = true, value = "角色名称"),
            @ApiImplicitParam(paramType = "query", name = "note", value = "备注信息，长度在 " + ValidationRule.REMARK_LENGTH + " 以内"),
            @ApiImplicitParam(paramType = "query", name = "sort", value = "排序号"),
            @ApiImplicitParam(paramType = "query", name = "menus", required = true, value = "菜单权限，菜单编号集合，存放角色有的菜单集合")
    })
    @PostMapping("/add")
    public void addRole(
            @RequestParam("roleCode") String roleCode,
            @Size(min = ValidationRule.ROLE_NAME_MIN, max = ValidationRule.ROLE_NAME_MAX, message = "{range}") @RequestParam("roleName") String roleName,
            @RequestParam(value = "note", required = false) String note, @RequestParam("sort") Integer sort,
            @RequestParam(value = "menus") Long[] menus) {
        roleService.addRole(roleCode,roleName,note,sort,menus);

    }

    @Log(content = "修改角色", logType = LogType.LOG_MANAGE)
    @ApiOperation(value = "修改角色")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "roleCode", required = true, value = "编号，待修改的角色编号"),
            @ApiImplicitParam(paramType = "query", name = "roleName", value = "名称，如果已有角色名称与当前名称一样，则返回 false"),
            @ApiImplicitParam(paramType = "query", name = "sort", value = "排序号"),
            @ApiImplicitParam(paramType = "query", name = "note", value = "备注信息，长度在 " + ValidationRule.REMARK_LENGTH + " 以内")
    })
    @PutMapping("/update/{roleCode}")
    public void updateRole(@PathVariable("roleCode") String roleCode,
                           @Size(min = ValidationRule.ROLE_NAME_MIN, max = ValidationRule.ROLE_NAME_MAX, message = "{range}") @RequestParam(value = "roleName", required = false) String roleName,
                           @RequestParam(value = "sort", required = false) Integer sort,
                           @RequestParam(value = "note", required = false) String note) {
        roleService.updateRole(roleCode,roleName,sort,note);
    }

    @Log(content = "删除角色", logType = LogType.LOG_MANAGE)
    @ApiOperation(value = "删除角色")
    @ApiImplicitParam(paramType = "query", name = "roles", required = true, value = "待删除的角色编号集合，多个编号之间逗号分割")
    @DeleteMapping("/deleteBatch")
    public void deleteRoleBatch(@RequestParam("roles") String[] roles){
        roleService.deleteRoleBatch(roles);

    }

    @ApiOperation(value = "查询角色有权限的菜单集合")
    @ApiImplicitParam(paramType = "path", name = "roleCode", required = true, value = "角色编号，用户查询指定角色的菜单编号集合")
    @GetMapping("/menus/{roleCode}")
    public List<Long> queryRoleMenuList(@PathVariable("roleCode") String roleCode) {
        return roleService.queryRoleMenuList(roleCode);
    }

    @Log(content = "修改角色菜单权限", logType = LogType.LOG_MANAGE)
    @ApiOperation(value = "修改角色菜单权限", notes = "修改角色菜单权限信息，将本次未勾选的菜单在角色上删除，将新勾选的菜单赋值给角色")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "roleCode", required = true, value = "角色编号，待修改的角色编号"),
            @ApiImplicitParam(paramType = "query", name = "menus", value = "菜单权限，菜单编号集合，存放角色有的菜单集合")
    })
    @PutMapping("/resetMenu/{roleCode}")
    public void setRoleMenus(@PathVariable("roleCode") String roleCode
            , @RequestParam(value = "menus", required = false) Long[] menus) {
        roleService.setRoleMenus(roleCode,menus);

    }

    @ApiOperation(value = "查询角色关联的管理员")
    @ApiImplicitParam(paramType = "path", name = "roleCode", required = true, value = "角色编号，用于查询该角色关联的管理员集合")
    @GetMapping("/admins/{roleCode}")
    public List<String> queryRoleAdminList(@PathVariable("roleCode") String roleCode) {
        return roleService.queryRoleAdminList(roleCode);
    }

    @ApiOperation(value = "导出角色列表", notes = "查询所有角色信息集合")
    @PostMapping("/list/export")
    public void export(HttpServletResponse response) throws IOException {
        List<RolePO> rolePOS = roleService.queryRoleList();
        ExcelUtil.exportExcel(rolePOS, "角色信息", "角色信息", RolePO.class, "角色信息", response);
    }

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }
}
